function OnSub		
	' Get provider name and type
	Dim g_objEnroll, g_objPrivateKey, g_objRequest, g_objCSPInformations
		
    Dim ProviderName, ProviderType
	Dim cn
	Dim theForm
	Dim pkcs10req
	Dim X500DistinguishedName
	Dim CspInformation, CspAlgorithms, CspAlgorithm, nBestIndex, nAlgIndex
		
	Dim p10lenght
		
'	OnSub = false
	On Error Resume Next
	GetCSR = ""
	Set theForm=Document.forms(0)
		
	Set g_objEnroll	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509Enrollment")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	Set g_objPrivateKey	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509PrivateKey")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	Set g_objRequest = window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX509CertificateRequestPkcs10")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	Set g_objCSPInformations	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CCspInformations")	
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	g_objCSPInformations.AddAvailableCsps
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	set options = document.forms(0).cryptProv.options
	index = options.selectedIndex
		
	ProviderName = options(index).text
	ProviderType = options(index).value
		
    g_objPrivateKey.ProviderName = ProviderName
		
    g_objPrivateKey.ProviderType = ProviderType
		
'	MsgBox ProviderName
'	MsgBox ProviderType
		
	  g_objPrivateKey.Length = 1024
		
    g_objPrivateKey.KeySpec = 2	'AT_SIGNATURE
		
		
    g_objPrivateKey.MachineContext = false
		
    g_objPrivateKey.KeyProtection = 1	' (XCN_NCRYPT_UI_PROTECT_KEY_FLAG = 1)
		
    g_objPrivateKey.ExportPolicy = 1	' (XCN_NCRYPT_ALLOW_EXPORT_FLAG = 1)
	  
	' Initialize
		
    Call g_objRequest.InitializeFromPrivateKey(1, g_objPrivateKey, "")	'X509CertificateEnrollmentContext.ContextUser
		
	Set X500DistinguishedName = window.parent.g_objClassFactory.CreateObject("X509Enrollment.CX500DistinguishedName")
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
	cn="cn=" & theForm.refNo.value
		
    Call X500DistinguishedName.Encode(cn, 0)	'XCN_CERT_NAME_STR_NONE
		
    g_objRequest.Subject = X500DistinguishedName
		
'	MsgBox cn
		
	' Set hash algo		
		
    Set CspInformation = g_objCSPInformations.ItemByName(ProviderName)
'	MsgBox cn
'	MsgBox Err.Number
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
'	MsgBox "Set CspInfomation"
	Set CspAlgorithms = CspInformation.CspAlgorithms
'	MsgBox Err.Number
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
		
'	MsgBox "Set CspAlgorithms"
'	nBestIndex = 0
'	Msgbox "CspAlgorithms.Count= " &CspAlgorithms.Count
		
'   For nAlgIndex=0 To CspAlgorithms.Count-1
'		MsgBox "CspAlgorithms.Name = " & CspAlgorithms.ItemByIndex(nAlgIndex).Name
'		If CspAlgorithms.ItemByIndex(nAlgIndex).Name = "md5" Then
'			nBestIndex = nAlgIndex
'		End If
'		If CspAlgorithms.ItemByIndex(nAlgIndex).Name = "md5" AND CspAlgorithms.ItemByIndex(nBestIndex).Name <> "sha1" Then
'			nBestIndex = nAlgIndex
'		End If
		
'       Next
'		MsgBox "nBestIndex= " & nBestIndex

'		Set CspAlgorithm = CspAlgorithms.ItemByIndex(nBestIndex)
'		MsgBox "before md5"
		Set CspAlgorithm = CspAlgorithms.ItemByName("md5")
		Set AlgorithmOid = CspAlgorithm.GetAlgorithmOid(0, 0)
'		MsgBox "CspAlgorithm.OID = " & AlgorithmOid.Value
'		MsgBox "after md5"
	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub=false
		window.event.returnvalue=false
		exit function
	End If
				
'		MsgBox "set md5"
		
	If CspAlgorithm.Type = 2 Then	
            'XCN_CRYPT_HASH_INTERFACE
			
    	g_objRequest.HashAlgorithm = AlgorithmOid	', AlgorithmFlagsNone
'		MsgBox "MD5 oid " & AlgorithmOid.Value
		
	End if
		
        
    ' Try to create request
		
'	MsgBox "InitializeFromRequest"
		
	g_objEnroll.InitializeFromRequest(g_objRequest)
		
'	MsgBox "CreateRequest"

'	pkcs10req = g_objEnroll.CreateRequest(3)	CRYPT_STRING_BASE64REQUESTHEADER
	pkcs10req = g_objEnroll.CreateRequest(3)	


	If ( err.number <> 0 ) then
		MsgBox Err.Number & " " & Err.Description
		OnSub = false
		window.event.returnvalue=false
		exit function
	End If		
		
	If(pkcs10req=Empty) Then
		errorString="The error '" & hex(Err.Number) & "' occurred."& chr(13) & chr(10) & "Your certificate request could not be generated."
		err=MsgBox(errorString,0,"Internet Explorer Certificate Request")
		MsgBox(errorString)
		window.event.returnvalue=false
		OnSub=false
		on error goto 0
		exit function
	End If
		
			
'	MsgBox "p10length = " & Len(pkcs10req)		
'	p10length = Len(pkcs10req) - 36 - 34			
'	MsgBox "p10length = " & p10length		
'	theForm.myKey.Value=Mid(pkcs10req , 37 , p10length)

	pkcs10req = Replace(pkcs10req, "-----BEGIN NEW CERTIFICATE REQUEST-----", " " )
	pkcs10req = Replace(pkcs10req, "-----END NEW CERTIFICATE REQUEST-----", " " )
  
  MsgBox pkcs10req
	theForm.myKey.Value=pkcs10req
	theForm.csp.Value=options(index).text
'		
'	MsgBox theForm.testp10.Value

End function


Sub FindProviders()
		On Error Resume Next
		Dim nDefaultCSP, nCSPIndex, CspInformations, CspInformation, oOption
		Dim g_objCSPInformations
'		MsgBox "Called FindProviders"
		Set g_objCSPInformations	= window.parent.g_objClassFactory.CreateObject("X509Enrollment.CCspInformations")	    
		'Set cspInfos = CreateObject("X509Enrollment.CCspInformations")
		If ( err.number <> 0 ) then
			MsgBox Err.Number & " " & Err.Description
			window.event.returnvalue=false
			'OnSub=false
			Exit Sub
		End If
'		MsgBox "Create Object OK!"
    
		g_objCSPInformations.AddAvailableCsps
		If ( err.number <> 0 ) then
			MsgBox Err.Number & " " & Err.Description
			'OnSub=false
			window.event.returnvalue=false
			Exit Sub
		End If
'		MsgBox "Add available csp OK!"
	    
		Set CspInformations = g_objCSPInformations
			
		nDefaultCSP = -1
		' Add error message if no CSPs are found
	    
'		MsgBox "Number of CSPs: " & CspInformations.Count 
	    
		If CspInformations.Count = 0 Then
			Set oOption=document.createElement("Option")
			oOption.text="N/A"
			document.all.cryptprov.add(oOption)
		Else 
			'Loop through all CspInformation objects
			For nCSPIndex = 0 To CspInformations.Count-1
				Set CspInformation = CspInformations.ItemByIndex(nCSPIndex)
			
            If CspInformation.Name <> "Microsoft Base Cryptographic Provider v1.0" Then			
					Set oOption = document.createElement("Option")
					oOption.text = CspInformation.Name
					oOption.Value = CspInformation.Type
					document.forms(0).cryptprov.add(oOption)
				end if
									
			Next
		End If 
		
'	  MsgBox "nDefaultCSP = " & nDefaultCSP 
	  
		If nDefaultCSP <> -1 Then
			Document.all.cryptprov.selectedIndex = nDefaultCSP
		Else
			Document.all.cryptprov.selectedIndex = 0	'Select first or N/A
		End If
	End Sub	'FindProviders	